package com.paic.mhis.hcpms.audit.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.ValidatorUtil;
import com.paic.mhis.hcpms.audit.biz.service.AuditProblemsService;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.web.BaseRest;

/**
 * 
 * @author EX-CAOTIANHUA001
 * 
 */
@Controller
public class AuditProblemsController extends BaseRest {

	@BizServicesProxy
	private AuditProblemsService auditProblemsService;

	@RequestMapping(value = "/hcpms/audit/queryList.do", method = RequestMethod.GET)
	public Map<String, Object> queryAuditList(HttpServletRequest request) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String page = request.getParameter("page");
			String pageSize = request.getParameter("pageSize");
			if (!isEmpty(page) || !isEmpty(pageSize)) {
				if (!ValidatorUtil.isPlusInteger(page)
						|| !ValidatorUtil.isPlusInteger(pageSize)) {
					HttpProcessUtil.generateExceptionResult(result,
							Constants.STATE_KEY,
							Constants.FAILED_VALIDATE_STATE_VALUE,
							Constants.MSG_KEY,
							Constants.PAGE_VALIDATE_FAILED_MSG);
					return result;
				}
			}
			JSONObject transferJson = new JSONObject();
			String[] property = { "problemCode", "orgName", "sourceType",
					"doctorName", "jobLicense", "problemStatus", "start",
					"end", "hearDateStart", "hearDateEnd", "page", "pageSize" };
			resetJSONObject(transferJson, request, property);
			result = auditProblemsService.queryAuditProblemsList(transferJson,
					result);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 事件登记列表查询
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/hcpms/audit/queryProblemList4Reg.do", method = RequestMethod.GET)
	public Map<String, Object> queryProblemList4Reg(HttpServletRequest request) {
		Map<String, Object> result = null;
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		try {
			int page = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));

			String problemCode = request.getParameter("problemCode");
			String orgName = request.getParameter("orgName");
			String jobLicense = request.getParameter("jobLicense");
			String hearDateStart = request.getParameter("hearDateStart");
			String hearDateEnd = request.getParameter("hearDateEnd");
			String doctorName = request.getParameter("doctorName");
			String sourceType = request.getParameter("sourceType");
			String start = request.getParameter("start");
			String end = request.getParameter("end");

			String problemStatus = request.getParameter("problemStatus");
			String sceneType = request.getParameter("sceneType");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("problemCode", problemCode);
			map.put("orgName", orgName);
			map.put("hearDateStart", hearDateStart);
			map.put("hearDateEnd", hearDateEnd);
			map.put("sourceType", sourceType);
			map.put("problemStatus", problemStatus);
			map.put("sceneType", sceneType);
			map.put("doctorName", doctorName);
			map.put("jobLicense", jobLicense);
			map.put("start", start);
			map.put("end", end);
			map.put("loginName", user.getUsername());
			result = auditProblemsService.queryProblemList4Reg(page, pageSize,
					map);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	@RequestMapping(value = "/hcpms/audit/queryCaseAuditList.do", method = RequestMethod.GET)
	public Map<String, Object> queryCaseAuditList(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			int page = Integer.parseInt(request.getParameter("page"));
			int pageSize = Integer.parseInt(request.getParameter("pageSize"));

			String caseCode = request.getParameter("caseCode");
			String orgName = request.getParameter("orgName");
			String beginDate = request.getParameter("beginDate");
			String endDate = request.getParameter("endDate");
			String sourceType = request.getParameter("sourceType");
			String doctorName = request.getParameter("doctorName");
			String jobLicense = request.getParameter("jobLicense");
			String caseStatus = request.getParameter("caseStatus");
			String start = request.getParameter("start");
			String end = request.getParameter("end");
			String doctorCode = request.getParameter("doctorCode");
			String point = request.getParameter("point");
			String clearDate = request.getParameter("clearDate");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("caseCode", caseCode);
			map.put("orgName", orgName);
			map.put("beginDate", beginDate);
			map.put("endDate", endDate);
			map.put("sourceType", sourceType);
			map.put("doctorName", doctorName);
			map.put("jobLicense", jobLicense);
			map.put("caseStatus", caseStatus);
			map.put("start", start);
			map.put("end", end);
			map.put("doctorCode", doctorCode);
			map.put("point", point);
			map.put("clearDate", clearDate);
			result = auditProblemsService.queryCaseAuditList(page, pageSize,
					map);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	private boolean isEmpty(String param) {
		return param == null || param.equals("");
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/hcpms/audit/saveCaseAudit.do", method = RequestMethod.POST)
	public Map<String, Object> saveAuditProblems(HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			transferJson.put("createdBy", user.getUsername());
			transferJson.put("updatedBy", user.getUsername());
			transferJson.put("submitUsername", user.getUsername());
			result = auditProblemsService.saveAuditProblems(transferJson, user);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	@RequestMapping(value = "/hcpms/audit/queryDetail.do", method = RequestMethod.GET)
	public Map<String, Object> queryAuditProblemsDetail(
			HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String auditProblemId = request.getParameter("auditProblemId");
			String problemCode = request.getParameter("problemCode");
			result = auditProblemsService.queryAuditProblemsDetail(
					auditProblemId, problemCode);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 
	 * @param jsonObject
	 * @param request
	 * @param property
	 */
	private void resetJSONObject(JSONObject transferJson,
			HttpServletRequest request, String... property) {
		for (String key : property) {
			transferJson.put(key, request.getParameter(key));
		}
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/hcpms/audit/batchEventsInitWf.do", method = RequestMethod.POST)
	public Map<String, Object> batchEventsInitWf(HttpServletRequest request) {
		UserDetails user = (UserDetails) request.getSession().getAttribute(
				"user");
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			transferJson.put("createdBy", user.getUsername());
			transferJson.put("updatedBy", user.getUsername());
			transferJson.put("submitUsername", user.getUsername());
			result = auditProblemsService.batchEventsInitWf(transferJson, user);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}
}
